package com.twang.stock.controller.common;

import com.twang.stock.dto.EbayRequest;
import com.twang.stock.ebay.AmazonService;
import com.twang.stock.service.jinyan.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping(value = "/amazon")
@Slf4j
public class AmazonController {

    @Autowired
    private AmazonService amazonService;

    @Autowired
    private CommonService commonService;

    @RequestMapping(value = "/")
    @ResponseBody
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("nameList", amazonService.getList());
        modelAndView.setViewName("amazon/index");
        return modelAndView;
    }

    @RequestMapping(value = "/detail")
    @ResponseBody
    public ModelAndView detail(String name) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("detailList", amazonService.getDetailList(name));
        modelAndView.setViewName("amazon/goodDetail");
        return modelAndView;
    }


    @RequestMapping(value = "/submit")
    @ResponseBody
    public ModelAndView submit() {
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("amazon/index");
        return modelAndView;
    }

    @RequestMapping(value = "/ebayApi")
    @ResponseBody
    public ModelAndView ebayApi(EbayRequest ebayRequest) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("nameList", amazonService.getList());
        modelAndView.setViewName("amazon/ebayApi");
        return modelAndView;
    }





    @RequestMapping(value = "/amazonSearch")
    @ResponseBody
    public void ebaySearchByTypeService(EbayRequest ebayRequest) {
        try {
            String totalPage = commonService.getBaseCommon("ebay_total_page").getCommonValue();
            ebayRequest.setGoodsUrl(ebayRequest.getGoodsUrl().replace("&ref=sr_pg_",""));
            int totalpage=Integer.valueOf(totalPage);
            for(int i=1;i<=totalpage;i++){
                amazonService.getAmazonPage(ebayRequest.getGoodsUrl(),i, ebayRequest.getUsername());
            }
        } catch (Exception e) {
            log.error("ebaySearchByTypeService.count exception={}", e);
        }
    }

}
